package sgi.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import sgi.details_new.MessageDetails;

public class SGIMessagesUI extends SGIClientFrame {
	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTable table;
	
	
	private static SGIMessagesController messages;
	/** constructor */
	public SGIMessagesUI() {
		
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 659, 525);		
		
		setTitle("Messages table");
		contentPane = new JPanel();
		getContentPane().setLayout(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/** return to useraccountmenu */
		JButton btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 450, 123, 36);
		getContentPane().add(btnReturn);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIMessagesController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		
		/** setting table  */
		Object[] columnNames = {"Title","Comment"};
		Object rowData[][]={};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames){
	      public boolean isCellEditable(int rowIndex, int mColIndex) {
	          return false;
	        }
	      };
		table = new JTable(dtm);
		table.setBounds(10, 127, 633, 321);
		
		
		JTableHeader header = table.getTableHeader();
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 107, 633, 332);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);

		getContentPane().add(panel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(table);
		
		
		/** setting table size  */
		TableColumn column=null;
		for(int i=0;i<table.getColumnCount(); i++)
		{
			column= table.getColumnModel().getColumn(i);
			if(i== 3)
			{

				column.setWidth(200);
			}
			if(i== 4)
			{

				column.setWidth(600);
			}
		}
	}

	/** set controller */
	public void setMessagesController(SGIMessagesController messages)
	{
		SGIMessagesUI.messages = messages;
	}
	
	/** adding rows to table */
	public void addRows(MessageDetails cmd)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {cmd.getTitle(),cmd.getComment()});
		table.setModel(dtm);
	}
	/** clear table */
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}	
	/** review table */
	public void review()
	{
		messages.ReviewMessages();	
	}
	
	
}
